/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package crm.clientManagement.managedbean;

import commonInfrastructure.system.entity.SystemParameterEntity;
import commonInfrastructure.system.session.SystemSessionBean;
import crm.clientManagement.entity.AddressEntity;
import crm.clientManagement.entity.CompanyEntity;
import crm.clientManagement.session.CompanySessionBean;
import crm.salesOrderProcessing.util.exception.IllegalCompanyTypeException;
import frm.creditControl.entity.CreditAccountEntity;
import frm.creditControl.session.CreditControlSessionBean;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;

/**
 *
 * @author GKnight
 */
@ManagedBean
@ViewScoped
public class ConvertManagedBean implements Serializable {

    private CompanyEntity salesLead;
    private String title1;
    private String title2;
    private CompanyEntity customer;
    private AddressEntity address = new AddressEntity();
    private List<String> billingAddressList = new ArrayList<String>();
    private List<String> shippingAddressList = new ArrayList<String>();
    private SystemParameterEntity systemParameter;
    private CreditAccountEntity creditAccount;
    @EJB
    private CompanySessionBean companySessionBean = new CompanySessionBean();
    @EJB
    private SystemSessionBean systemSessionBean;
    @EJB
    private CreditControlSessionBean creditControlSessionBean;

    /** Creates a new instance of ConvertManagedBean */
    public ConvertManagedBean() {
        if (getSalesLead() == null) {
            salesLead = (CompanyEntity) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("converting");
            if (salesLead == null){
                System.out.println("&&&&&&&&&&Null");
            }
        }
        title1 = "Billing Addresses for "+salesLead.getName();
        title2 = "Shipping Addresses for "+salesLead.getName();
        
        customer = salesLead;
        System.out.println("&&&&&&&&&&&&"+customer.getName());
    }

    public void initView(PhaseEvent event) {

        if (getSalesLead() == null) {

            setSalesLead((CompanyEntity) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("converting"));
        }


        title1 = "Billing Addresses for " + salesLead.getName();
        title2 = "Shipping Addresses for " + salesLead.getName();

        customer = salesLead;
        System.out.println("&&&&&&&&&&&&"+customer.getName());
    }

    public void addToBilling(ActionEvent event) {

        if (address.getStreetName().equals("") || address.getCity().equals("") || address.getPostCode().equals("")) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Please fill in all fields", ""));
            return;
        } else {
            billingAddressList.add(address.getStreetName() + " ," + address.getCity() + " ," + address.getPostCode());
            address = new AddressEntity();
        }
    }

    public void deleteBilling(String selectedBilling) {
        billingAddressList.remove(selectedBilling);
    }

    public void addToShipping(ActionEvent event) {

        if (address.getStreetName().equals("") || address.getCity().equals("") || address.getPostCode().equals("")) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Please fill in all fields", ""));
            return;
        } else {
            shippingAddressList.add(address.getStreetName() + " ," + address.getCity() + " ," + address.getPostCode());
            address = new AddressEntity();
        }
    }

    public void deleteShipping(String selectedShipping) {
        shippingAddressList.remove(selectedShipping);
    }

    public void saveCustomer(ActionEvent event) throws IOException{

        systemParameter = systemSessionBean.getSystemParameter();

        for (int i = 0; i < billingAddressList.size(); i++) {
            String[] billing = billingAddressList.get(i).split(",");
            address.setStreetName(billing[0]);
            address.setCity(billing[1]);
            address.setPostCode(billing[2]);
            customer.getBillingAddresses().add(address);
            address = new AddressEntity();
        }
        for (int i = 0; i < shippingAddressList.size(); i++) {
            String[] shipping = shippingAddressList.get(i).split(",");
            address.setStreetName(shipping[0]);
            address.setCity(shipping[1]);
            address.setPostCode(shipping[2]);
            customer.getShippingAddresses().add(address);
            address = new AddressEntity();
        }

        customer.setCompanyStatus(systemParameter.getFullCustomerCompanyStatus());
        customer.setIsCustomer(true);

        try {
            creditAccount = creditControlSessionBean.createNewCreditAccount(customer);
        } catch (IllegalCompanyTypeException e) {
            
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "This company type does not exist.", ""));
        }
        
        customer.setCreditAccount(creditAccount);


        companySessionBean.saveChanges(customer);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Save Successfully", ""));
//        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Save Successfully", "");
//        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("msgUpdate", msg);
        FacesContext.getCurrentInstance().getExternalContext().redirect("./deleteCompany.xhtml");
    }

    public void goBack() throws IOException {
        FacesContext.getCurrentInstance().getExternalContext().redirect("./deleteCompany.xhtml");
    }

    /**
     * @return the salesLead
     */
    public CompanyEntity getSalesLead() {
        return salesLead;
    }

    /**
     * @param salesLead the salesLead to set
     */
    public void setSalesLead(CompanyEntity salesLead) {
        this.salesLead = salesLead;
    }

    /**
     * @return the title1
     */
    public String getTitle1() {
        return title1;
    }

    /**
     * @param title1 the title1 to set
     */
    public void setTitle1(String title1) {
        this.title1 = title1;
    }

    /**
     * @return the title2
     */
    public String getTitle2() {
        return title2;
    }

    /**
     * @param title2 the title2 to set
     */
    public void setTitle2(String title2) {
        this.title2 = title2;
    }

    /**
     * @return the customer
     */
    public CompanyEntity getCustomer() {
        return customer;
    }

    /**
     * @param customer the customer to set
     */
    public void setCustomer(CompanyEntity customer) {
        this.customer = customer;
    }

    /**
     * @return the address
     */
    public AddressEntity getAddress() {
        return address;
    }

    /**
     * @param address the address to set
     */
    public void setAddress(AddressEntity address) {
        this.address = address;
    }

    /**
     * @return the billingAddressList
     */
    public List<String> getBillingAddressList() {
        return billingAddressList;
    }

    /**
     * @param billingAddressList the billingAddressList to set
     */
    public void setBillingAddressList(List<String> billingAddressList) {
        this.billingAddressList = billingAddressList;
    }

    /**
     * @return the shippingAddressList
     */
    public List<String> getShippingAddressList() {
        return shippingAddressList;
    }

    /**
     * @param shippingAddressList the shippingAddressList to set
     */
    public void setShippingAddressList(List<String> shippingAddressList) {
        this.shippingAddressList = shippingAddressList;
    }
}
